﻿using System;

namespace Relay.Core.Protocol {
    /// <summary>
    /// Represents the parts of a raw IRC server message.
    /// </summary>
    public class IrcServerMessage {
        #region Fields
        private string _source = String.Empty;
        private string _command = String.Empty;
        private string _params = String.Empty;
        #endregion

        #region Constructor
        public IrcServerMessage(string source, string command, string paramList) {
            _source = source;
            _command = command;
            _params = paramList;
        }

        public IrcServerMessage(string rawMessage) {
            // An opening colon means it has a source prefix
            bool hasPrefix = rawMessage.StartsWith(":");
            string[] messageParts = rawMessage.Split(new char[] { ' ' }, hasPrefix ? 3 : 2);

            if (hasPrefix) {
                _source = messageParts[0];
                _command = messageParts[1];
                _params = messageParts[2];
            } else {
                _command = messageParts[0];
                _params = messageParts[1];
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// The originating source of this message.
        /// </summary>
        public string Source {
            get { return _source; }
            protected set { _source = value; }
        }
        /// <summary>
        /// The command that is being sent.
        /// </summary>
        public string Command {
            get { return _command; }
            protected set { _command = value; }
        }
        /// <summary>
        /// The trailing parameters of the message.
        /// </summary>
        public string Params {
            get { return _params; }
            protected set { _params = value; }
        }
        #endregion
    }
}
